package com.lxmes.gs.controller;

import java.util.List;
import cn.hutool.core.bean.BeanUtil;
import com.lxmes.gs.domain.myTaskVO.*;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lxmes.common.annotation.Log;
import com.lxmes.common.core.controller.BaseController;
import com.lxmes.common.core.domain.AjaxResult;
import com.lxmes.common.enums.BusinessType;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.lxmes.gs.domain.MyTask;
import com.lxmes.gs.service.IMyTaskService;
import com.lxmes.common.utils.poi.ExcelUtil;
import com.lxmes.common.core.page.TableDataInfo;

/**
 * 我的任务Controller
 * 
 * @author wdf
 * @date 2023-04-24
 */
@Api(tags = "我的任务")    
@RestController
@RequestMapping("/gs/myTask")
public class MyTaskController extends BaseController{
    @Autowired
    private IMyTaskService myTaskService;

    /**
     * 查询我的任务列表
     */
    @ApiOperation("查询我的任务列表")
    @PreAuthorize("@ss.hasPermi('gs:myTask:list')")
    @GetMapping("/list")
    public TableDataInfo list(MyTaskSearchVO myTask){
        startPage();
        List<MyTask> list = myTaskService.selectMyTaskList(BeanUtil.copyProperties(myTask,MyTask.class));
        return getDataTable(Convert.convert(new TypeReference<List<MyTaskResultVO>>() {},list));
}

    /**
     * 导出我的任务列表
     */
    @ApiOperation("导出我的任务列表")
    @PreAuthorize("@ss.hasPermi('gs:myTask:export')")
    @Log(title = "我的任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MyTask myTask){
        List<MyTask> list = myTaskService.selectMyTaskList(myTask);
        List<MyTaskExportVO> exportList = Convert.convert(new TypeReference<List<MyTaskExportVO>>() {}, list);
        ExcelUtil<MyTaskExportVO> util = new ExcelUtil<MyTaskExportVO>(MyTaskExportVO.class);
        util.exportExcel(response, exportList, "我的任务数据");
    }

    /**
     * 获取我的任务详细信息
     */
    @ApiOperation("获取我的任务详细信息")
    @PreAuthorize("@ss.hasPermi('gs:myTask:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id){
        return AjaxResult.success(myTaskService.selectMyTaskById(id));
    }

    /**
     * 新增我的任务
     */
    @ApiOperation("新增我的任务")
    @PreAuthorize("@ss.hasPermi('gs:myTask:add')")
    @Log(title = "我的任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MyTaskCreateVO myTask){
        return toAjax(myTaskService.insertMyTask(BeanUtil.copyProperties(myTask,MyTask.class)));
    }

    /**
     * 修改我的任务
     */
    @ApiOperation("修改我的任务")
    @PreAuthorize("@ss.hasPermi('gs:myTask:edit')")
    @Log(title = "我的任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MyTaskUpdateVO myTask){
        return toAjax(myTaskService.updateMyTask(BeanUtil.copyProperties(myTask,MyTask.class)));
    }

    /**
     * 删除我的任务
     */
    @ApiOperation("删除我的任务")
    @PreAuthorize("@ss.hasPermi('gs:myTask:remove')")
    @Log(title = "我的任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids){
        return toAjax(myTaskService.deleteMyTaskByIds(ids));
    }
    /**
     * 接收我的任务
     */
    @ApiOperation("接收我的任务")
    @PreAuthorize("@ss.hasPermi('gs:myTask:receive')")
    @Log(title = "接收我的任务")
	@PostMapping("/receiveMyTask/{ids}")
    public AjaxResult receiveMyTask(@PathVariable Long[] ids){
        return toAjax(myTaskService.receiveMyTask(ids));
    }
    /**
     * 导入我的任务
     */
    @Log(title = "我的任务", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception{
        ExcelUtil<MyTaskImportVO> util = new ExcelUtil<>(MyTaskImportVO.class);
        List<MyTaskImportVO> myTaskList = util.importExcel(file.getInputStream());
        String message = myTaskService.imporIteminfo(Convert.convert(new TypeReference<List<MyTask>>() {},myTaskList), updateSupport);
        return AjaxResult.success(message);
    }
    /**
     * 下载导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException{
        ExcelUtil<MyTaskImportVO> util = new ExcelUtil<>(MyTaskImportVO.class);
        util.importTemplateExcel(response,"我的任务");
    }
}
